<script>
import Vue from 'vue'

function extend(to, _from) {
  for (let key in _from) {
    to[key] = _from[key]
  }
  return to
}

const compiler = require('vue-template-compiler')
import { componentJson, parseComponentByJson, parseComponentByStr } from '../util/templateUtil.js'

export default {
  data() {
    return {
      app: 'res'
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      // 构建组件
      let { component, sourceCode } = parseComponentByJson(componentJson)
      // let res = compiler.com(sourceCode)
      let config = parseComponentByStr(sourceCode)
      if (Vue.component('res1')) {
        debugger
      }
      // extend(res, component.script)
      Vue.component('res1', config.component)
      if (Vue.component('res1')) {
        debugger
      }
    },

    renderPreview() {
      const { hasError, errorMessage } = this
      // <div>{this.initialExample ? this.initialExample : <div>Loading...</div>}</div>
    }
  },
  render() {
    let { component } = parseComponentByJson(componentJson)
    extend(component, component.script)
    const renderComponent = component
    return (
      <div class="code-view">
        <res1></res1>
      </div>
    )
  }
}
</script>
